package com.uniledger.exchange.wallet.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.uniledger.commonlib.view.FullGridView;
import com.uniledger.exchange.R;
import com.uniledger.exchange.wallet.MnemonicAdapter;
import com.uniledger.exchange.wallet.WalletKey;

import org.reactivestreams.Subscription;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

import static android.content.Context.CLIPBOARD_SERVICE;

/**
 * 备份助记词.
 */
public class BackupFragment extends MnemonicFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private WalletKey walletKey;
    private String mParam2;

    @BindView(R.id.txt_words)
    FullGridView words;

    @BindView(R.id.btn_copy)
    Button btnCopy;

    ClipboardManager myClipboard;

    public BackupFragment() {
        // Required empty public constructor
    }


    public static BackupFragment newInstance(WalletKey walletKey, String param2) {
        BackupFragment fragment = new BackupFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, walletKey);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            walletKey = getArguments().getParcelable(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        myClipboard = (ClipboardManager) getActivity().getSystemService(CLIPBOARD_SERVICE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_backup, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @BindString(R.string.mnemonic_btn_next)
    String btn_next;

    @Override
    public void onResume() {
        super.onResume();
        listener.getButton().setText(btn_next);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btnCopy.setOnClickListener(this::onClick);
        List<String> list = Arrays.asList(walletKey.mnemonic.split("\\s+"));
        MnemonicAdapter adapter = new MnemonicAdapter(list, getActivity());
        words.setAdapter(adapter);
    }

    @BindString(R.string.mnemonic_btn_copy)
    String btn_copy;

    @BindString(R.string.mnemonic_btn_copied)
    String btn_copied;

    @Override
    public void onClick(View v) {
        if (v.getId() == listener.getButton().getId()) {
            listener.backup(walletKey);
        }

        if (v.getId() == R.id.btn_copy) {
            String text = walletKey.mnemonic;
            ClipData myClip = ClipData.newPlainText("text", text);
            myClipboard.setPrimaryClip(myClip);
            Flowable.timer(1, TimeUnit.SECONDS)
                    .doOnSubscribe(new Consumer<Subscription>() {
                        @Override
                        public void accept(Subscription subscription) throws Exception {
                            btnCopy.setText(btn_copied);
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) throws Exception {
                            btnCopy.setText(btn_copy);
                        }
                    });
        }

    }


}
